//
//  VideoTableViewCell.m
//  OneDay
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 oneDay. All rights reserved.
//

#import "VideoTableViewCell.h"

@implementation VideoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubviews];
    }
    return self;
}

// 添加子视图
- (void)addSubviews
{
    CGFloat kSpace = kWidth / 32;
    self.bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kSpace, 5, kWidth - 2 * kSpace, kHeight / 2.7)];
    self.bgImageView.layer.cornerRadius = self.bgImageView.height / 20;
    self.bgImageView.layer.masksToBounds = YES;
    self.bgImageView.userInteractionEnabled = YES;
    [self addSubview:self.bgImageView];
    
    for (int i = 0; i < 70; i++) {
        CGFloat num = 1.0 / 70 *(70 - i);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, _bgImageView.height - i, _bgImageView.width, 1)];
        view.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0  alpha:num];
        [_bgImageView addSubview:view];
    }
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(4 * kSpace,  self.bgImageView.height / 1.4, self.bgImageView.width / 1.5, self.bgImageView.height / 8.3)];
    self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 256.0 green:arc4random() % 255 / 256.0 blue:arc4random() % 255 / 256.0 alpha:0.3];
    [self.bgImageView addSubview:self.titleLabel];
    
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.bgImageView.width / 2 - 50, self.titleLabel.bottom, self.bgImageView.width / 5, 30)];
    self.timeLabel.textColor = [UIColor whiteColor];
    [self.bgImageView addSubview:self.timeLabel];
    
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectionButton.frame = CGRectMake(self.timeLabel.right + 5, self.timeLabel.top + 5, 20, 20);
    [self.collectionButton addTarget:self action:@selector(collectionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgImageView addSubview:self.collectionButton];
    self.collectionLabel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectionLabel.frame = CGRectMake(self.collectionButton.right, self.timeLabel.top, self.bgImageView.height / 5, 30);
    [self.collectionLabel addTarget:self action:@selector(collectionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.collectionLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.bgImageView addSubview:self.collectionLabel];
    
    self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareButton.frame = CGRectMake(self.collectionLabel.right + 5, self.timeLabel.top + 5, 20, 20);
    [self.bgImageView addSubview:self.shareButton];
    self.shareLabel = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareLabel.frame = CGRectMake(self.shareButton.right, self.timeLabel.top, self.bgImageView.height / 5, 30);
    [self.shareLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.bgImageView addSubview:self.shareLabel];
}

- (void)setModel:(VideoModel *)model
{
    if (_model != model) {
        _model = model;
    }
    [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:self.model.coverForDetail] placeholderImage:[UIImage imageNamed:@"oneDay.jpg"]];
    self.titleLabel.text = self.model.title;
    self.timeLabel.text = [NSString stringWithFormat:@"%.2f分钟", self.model.duration / 60.0];
    [self.collectionButton setImage:[UIImage imageNamed:@"iconfont-xihuan"] forState:UIControlStateNormal];
    [self.collectionLabel setTitle:[NSString stringWithFormat:@"%@", self.model.consumption[@"collectionCount"]] forState:UIControlStateNormal];
    [self.shareButton setImage:[UIImage imageNamed:@"iconfont-fenxiang-2"] forState:UIControlStateNormal];
    [self.shareLabel setTitle:[NSString stringWithFormat:@"%@", self.model.consumption[@"shareCount"]] forState:UIControlStateNormal];
}

- (void)collectionBtnClick
{
    if ([self.delegate respondsToSelector:@selector(collectionCellDidSelected:)]) {
        [self.delegate collectionCellDidSelected:self.model];
    }
}

@end
